﻿using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using NPOI.HSSF.UserModel;

namespace LDSCore
{
    /// <summary>
    /// 工具类
    /// </summary>
    public class Utils
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string EncryptMd5(string value)
        {
            var md5 = new MD5CryptoServiceProvider();
            var data = System.Text.Encoding.Default.GetBytes(value);//将字符编码为一个字节序列 
            var md5Data = md5.ComputeHash(data);//计算data字节数组的哈希值 
            md5.Clear();
            var str = "";
            for (var i = 0; i < md5Data.Length - 1; i++)
            {
                str += md5Data[i].ToString("x").PadLeft(2, '0');
            }
            return str;
        }

        /// <summary>
        /// 对象克隆
        /// </summary>
        /// <typeparam name="TEntity">对象类型</typeparam>
        /// <param name="source">对象</param>
        /// <returns>返回克隆的对象</returns>
        public static TEntity Clone<TEntity>(TEntity source)
        {
            // Don’t serialize a null object, simply return the default for that object
            if (ReferenceEquals(source, null))
            {
                return default(TEntity);
            }
            var dcs = new DataContractSerializer(typeof(TEntity));
            using (Stream stream = new MemoryStream())
            {
                dcs.WriteObject(stream, source);
                stream.Seek(0, SeekOrigin.Begin);
                return (TEntity)dcs.ReadObject(stream);
            }
        }

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="dtSource">数据源</param>
        /// <param name="fileName"></param>
        /// <returns>返回Excel路径</returns>
        public static bool ExportExcel(DataTable dtSource, string fileName)
        {
            //if (dtSource == null || dtSource.Rows.Count <= 0) return false;
            if (dtSource == null) return false;

            var workbook = new HSSFWorkbook();
            var sheet = workbook.CreateSheet();

            //填充表头   
            var dataRow = sheet.CreateRow(0);
            foreach (DataColumn column in dtSource.Columns)
            {
                dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
            }

            //填充内容   
            for (int i = 0; i < dtSource.Rows.Count; i++)
            {
                dataRow = sheet.CreateRow(i + 1);
                for (int j = 0; j < dtSource.Columns.Count; j++)
                {
                    dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i][j].ToString());
                }
            }
            
            //保存 
            using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                workbook.Write(fs);
            }

            Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

            return true;
        }

        public static bool Same(Image image1, Image image2)
        {
            if (image1 == null || image2 == null) return false;

            var ms1 = new MemoryStream();
            var ms2 = new MemoryStream();

            image1.Save(ms1, System.Drawing.Imaging.ImageFormat.Bmp);
            image2.Save(ms2, System.Drawing.Imaging.ImageFormat.Bmp);
            var im1 = ms1.GetBuffer();
            var im2 = ms2.GetBuffer();
            if (im1.Length != im2.Length)
                return false;
            return !im1.Where((t, i) => t != im2[i]).Any();
        }
    }
}
